﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CMS_Admin_ViewContent : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                Bind();
            }
            catch
            {
            }
        }
    }




    private void Bind()
    {
        string type = Request["type"].ToString();
        string id = Request["id"].ToString();

        switch (type)
        {
            case "a":
                if (Request["action"] != null)
                {
                    ViewControl1.AddMode(Usas.Common.Cms.Role.RedType.Article, id);
                    DoAdd.Visible = true;
                    GetArtMode(CMS_Control_ViewControl.ShowMode.IsView);
                    Add.Attributes.Add("onclick", "return confirm('确认执行？')");
                }
                else
                {

                    CMS_Control_ViewControl.ShowMode SM = new CMS_Control_ViewControl.ShowMode();



                    if (Request["check"] != null)
                    {
                        Usas.Common.Cms.Role ro = new Usas.Common.Cms.Role();
                        if (Request["check"].ToString() != ro.GetMd5Str32(Request["id"].ToString() + "check"))
                        {
                            Cont.InnerHtml = "<b>非法访问</b>";
                            //SM = ViewControl1.SetValue(Convert.ToInt32(id), false);
                        }
                        else
                        {
                            SM = ViewControl1.SetValue(Convert.ToInt32(id), true);
                            GetArtMode(SM);
                        }
                    }
                    else
                    {
                        SM = ViewControl1.SetValue(Convert.ToInt32(id), false);
                        GetArtMode(SM);
                    }




                }
                //{
                //    DoAdd.Visible = true;
                //    Cont.Visible = true;
                //    Dir.Visible = false;
                //    DoSave.Visible = false;
                //    DoCheck.Visible = false;
                //    bool ck = Request["ck"].ToString() == "1" ? true : false;
                //    if (ck)
                //    {
                //        Add.Attributes.Add("onclick", "return confirm('确认执行？')");
                //        Public.Attributes.Add("onclick", "return confirm('此篇文章需要验证,确认执行？')");
                //    }
                //    else
                //    {
                //        Add.Attributes.Add("onclick", "return confirm('确认执行？')");
                //    }
                //    ViewControl1.AddMode();

                //}
                //else
                //{
                //    CMS_Control_ViewControl.ShowMode SM = ViewControl1.SetValue(Convert.ToInt32(Request["id"]), Request["e"].ToString(), Request["v"].ToString(), Request["ck"].ToString(), Request["ck"].ToString(), Request["isf"].ToString(), Request["fid"].ToString());
                //    ViewControl1.ControlInit(true);

                //    GetArtMode(SM);

                //    string IsCheck = ViewControl1.NowIsCheck ? "\\r该内容将进行审核" : "";
                //    Save.Attributes.Add("onclick", "return confirm('确认执行？')");
                //    Public.Attributes.Add("onclick", "return confirm('确认执行？" + IsCheck + "')");
                //    AddVersion.Attributes.Add("onclick", "return confirm('确认执行')");

                //    Dir.Visible = false;
                //}
                Dir.Visible = false;
                break;
            case "b":
                if (Request["action"] != null)
                {
                    ViewControl1.AddMode(Usas.Common.Cms.Role.RedType.Border, Request["b"].ToString());
                    DoAdd.Visible = true;
                    GetArtMode(CMS_Control_ViewControl.ShowMode.IsView);
                    PartControl1.Visible = false;
                    Add.Attributes.Add("onclick", "return confirm('确认执行？')");
                }
                else
                {
                    Cont.Visible = false;
                    PartControl1.IntiPart(Usas.Common.Cms.Role.RedType.Border, Convert.ToInt32(Request["id"]));
                }
                break;
            case "t":
                Cont.Visible = false;
                PartControl1.IntiPart(Usas.Common.Cms.Role.RedType.Type, Convert.ToInt32(Request["id"]));
                break;
        }
    }

    private void GetArtMode(CMS_Control_ViewControl.ShowMode SM)
    {

        switch (SM)
        {
            case CMS_Control_ViewControl.ShowMode.IsCheck:
                DoCheck.Visible = true;
                DoSave.Visible = true;
                Save.Attributes.Add("onclick", "return confirm('是否保存？')");
                Check.Attributes.Add("onclick", "return confirm('是否审核？')");
                ViewControl1.DelVis(false);
                break;
            case CMS_Control_ViewControl.ShowMode.IsEdit:
                DoCheck.Visible = false;
                DoSave.Visible = true;
                DoPublick.Visible = false;
                Save.Attributes.Add("onclick", "return confirm('是否保存？')");
                AddVersion.Attributes.Add("onclick", "return confirm('确认添加？')");
                break;
            case CMS_Control_ViewControl.ShowMode.IsVersion:
                DoCheck.Visible = false;
                DoSave.Visible = false;
                ViewControl1.DelVis(false);
                break;
            case CMS_Control_ViewControl.ShowMode.IsView:
                DoCheck.Visible = false;
                DoSave.Visible = false;
                ViewControl1.DelVis(false);
                DoPublick.Visible = false;
                AddVersion.Visible = false;
                break;
            case CMS_Control_ViewControl.ShowMode.IsNotPublic:
                Usas.BLL.Cms.cmsArticle ar = new Usas.BLL.Cms.cmsArticle();
                Usas.Model.Cms.cmsArticle arm = new Usas.Model.Cms.cmsArticle();
                Usas.BLL.Cms.cmsBorder bo = new Usas.BLL.Cms.cmsBorder();
                Usas.Model.Cms.cmsBorder bom = new Usas.Model.Cms.cmsBorder();

                bom = bo.GetModel(ar.GetModel(Convert.ToInt32(Request["id"])).BorderId);

                if (bom.IsCheck)
                {
                    Public.Attributes.Add("onclick", "return confirm('该内容需要【审核】 是否发布？')");
                    Public.CommandArgument = true.ToString();
                    Public.CommandName = bom.CheckPerson.ToString();
                }
                else
                {
                    Public.Attributes.Add("onclick", "return confirm('是否发布？')");
                    Public.CommandArgument = false.ToString();
                    Public.CommandName = bom.CheckPerson.ToString();
                }

                DoSave.Visible = true;

                Save.Attributes.Add("onclick", "return confirm('是否保存？')");

                DoPublick.Visible = true;
                AddVersion.Visible = false;
                DoCheck.Visible = false;
                break;
            case CMS_Control_ViewControl.ShowMode.IsDel:
                DoCheck.Visible = false;
                DoSave.Visible = false;
                DoPublick.Visible = false;
                AddVersion.Visible = false;
                DoSave.Visible = true;
                Save.Attributes.Add("onclick", "return confirm('是否保存？')");

                break;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        try
        {
            string ValStr = ViewControl1.Val();
            if (ValStr.Length <= 0)
            {
                ViewControl1.Save();
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('修改成功');location.href='" + Request.Url.ToString() + "'", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('" + ValStr + "')", true);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

    protected void AddVersion_Click(object sender, EventArgs e)
    {
        string ValStr = ViewControl1.Val();
        if (ValStr.Length <= 0)
        {
            int Nid = ViewControl1.AddVersion();
            if (Nid > 0)
            {
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('增加版本成功');location.href='ViewContent.aspx?id=" + Nid + "&type=a'", true);
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('添加出错！')", true);
            }

        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('" + ValStr + "')", true);
        }
    }

    protected void Check_Click(object sender, EventArgs e)
    {


        string ValStr = ViewControl1.Val();
        if (ValStr.Length <= 0)
        {
            ViewControl1.PassCheck();
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('修改成功');parent.location.reload()", true);

        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('" + ValStr + "')", true);
        }
    }

    protected void Add_Click(object sender, EventArgs e)
    {
        string ValStr = ViewControl1.Val();
        if (ValStr.Length <= 0)
        {
            int nid = ViewControl1.Add(Convert.ToInt32(Request["t"]), Convert.ToInt32(Request["b"]), Convert.ToInt32(Request["id"]));
            if (nid > 0)
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('添加成功');location.href='ViewContent.aspx?type=a&id=" + nid + "'", true);

        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('" + ValStr + "')", true);
        }
    }

    protected void Public_Click(object sender, EventArgs e)
    {
        string ValStr = ViewControl1.Val();
        if (ValStr.Length <= 0)
        {
            bool back = ViewControl1.ArticlePublic(Convert.ToInt32(Request["id"].ToString()), Convert.ToBoolean(Public.CommandArgument), Convert.ToInt32(Public.CommandName));
            if (back)
            {
                if (Request["check"] != null)
                {
                    ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('发布成功');window.close()", true);
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('发布成功,重新打开页面获取最新数据');document.getElementById('vc').style.display = 'none';document.write('发布成功')", true);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "Msg", "location.href='" + Request.Url.ToString() + "'", true);
            }
        }
        else
        {
            ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('" + ValStr + "')", true);
        }

    }
}
